
var app = require('../../server/server');
module.exports = function(Jjdb) {

	var type = {
	    '10000':'案件', '20000':'交通', '30000':'火警', '40000':'求助', '50000':'咨询',
	    '60000':'举报', '70000':'投诉监督', '80000':'事件', '90000':'灾害事故', 
	    '100000':'纠纷', '110000':'警务协助', '999000':'其它报警', '10':'刑事案件', 
	    '20':'治安案件', '120000':'医疗救助', '130000':'扬言类', 'NULL':'NULL'
	};

	Jjdb.getCurrentJQ = function (cb) {
		var now = new Date();
		now.setHours(now.getHours() - 2);
		var option = {
      where: {
        and: [
          {BJSJ: {gt: now}},
          {AFDD: {neq: null}},
          {DHDWJD: {gt: '0'}},
          {BJNR: {neq: null}}
        ]
      },
      order: 'BJSJ desc'
    };
    var result = [];
    Jjdb.find(option, function(err, data) {
    	for(var i = 0; i < data.length; i++) {
    		// data[i].BJSJ.setHours(data[i].BJSJ.getHours() + 8);
    		var bdjwd = app.models.util.bd_encrypt(data[i].DHDWWD - 0, data[i].DHDWJD - 0);
        result.push({
          type: 'case',
          msg: data[i].BJNR,
          addr: data[i].AFDD,
          lat: bdjwd[0],
          lng: bdjwd[1],
          time: data[i].BJSJ,
          lb: type[data[i].BJLBDM]
          // distance: Math.floor(Math.abs(data[i].DHDWWD - lat) * 110000 
          //   + Math.abs(data[i].DHDWJD - lng) * 94000)
        });
      }
      cb(null, result);
    })

	};

	Jjdb.remoteMethod(
    'getCurrentJQ', {
      http: {path: '/getCurrentJQ', verb: 'post'},
      accepts: [
      ],
      returns: {type: 'array', root: true}
    }
  );

   Jjdb.searchData = function (searchText, lat, lng, cb) {
   	var now = new Date();
   	now.setDate(now.getDate() - 7);
    var option = {
      where: {
        and: [
          {BJNR: {like: '%' + searchText + '%'}},
          {BJSJ: {gt: now}}
        ]
      }
    };
    var result = [];
    Jjdb.find(option, function (err, data) {
      for(var i = 0; i < data.length; i++) {
      	// data[i].BJSJ.setHours(data[i].BJSJ.getHours() + 8);
      	var bdjwd = app.models.util.bd_encrypt(data[i].DHDWWD - 0, data[i].DHDWJD - 0);
        result.push({
          type: 'case',
          msg: data[i].BJNR,
          addr: data[i].AFDD,
         	lat: bdjwd[0],
          lng: bdjwd[1],
          time: data[i].BJSJ,
          lb: type[data[i].BJLBDM],
          // distance: Math.floor(Math.abs(bdjwd[0] - lat) * 110000 
          //   + Math.abs(bdjwd[1] - lng) * 94000)
        })
      }
      cb(null, result);
    })
  };

  Jjdb.remoteMethod(
    'searchData', {
      http: {path: '/searchData', verb: 'post'},
      accepts: [
        {arg: 'searchText', type: 'string', required: 'true'},
        {arg: 'lat', type: 'number', required: 'true'},
        {arg: 'lng', type: 'number', required: 'true'}
      ],
      returns: {type: 'array', root: true}
    }
  );

};
